/**
 * 
 * 
 * */

import $store from '@/store/index.js'

export default {
	// 全局配置
	common: {
		// baseUrl: "http://127.0.0.1:7001",
		baseUrl: "http://m1btc.vipgz1.idcfengye.com",
		data: {},
		method: 'GET',
		get(url, data = {}, options = {}) {
			options.url = url
			options.data = data
			options.method = 'GET'
			return this.request(options)
		},

	},
	// 请求 返回promise
	request(options = {}) {
		// 组织参数
		options.url = this.common.baseUrl + options.url;
	
 
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		
		// 请求之前... todo
		// token
		// if (options.token) {
			// console.log('$store.state.user.userInfo', $store.state.user.userInfo)
			// options.data.btc_token = $store.state.user.userInfo.token;
			// options.data.user_id = $store.state.user.userInfo.id;
			console.log('$store.state.user',$store.state.user)
			options.header = {
				'btc_token': $store.state.user.token,
			}
			// 二次验证
			console.log('options', options)
			if (options.checkToken && !options.data.btc_token) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				return uni.navigateTo({
					url: '/pages/login/login',
				});
			}
		// }

		// 请求
		return new Promise((res, rej) => {
			// 请求中...
			uni.request({
				...options,
				success: (result) => {

					console.log('result', result)

					res(result.data)
				},
				fail: (error) => {
					console.log(error)
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej()
				}
			});
		})
	},


	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST';

		return this.request(options)
	},
	
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET';
	
		return this.request(options)
	},

}
